package com.itheima.controller;

import com.itheima.anno.Log;
import com.itheima.mapper.DeptMapper;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Emp;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门管理Controller
 */
@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {

//    private static Logger log = LoggerFactory.getLogger(DeptMapper.class);



    //注入service对象
    @Autowired
    private DeptService deptService;

    /**
     * 查询部门数据
     * @return
     */
    //    @RequestMapping(value = "/depts",method = RequestMethod.GET)
    @GetMapping
    public Result list(){
        log.info("查询全部部门数据");

        //调用service查询部门数据
        List<Dept> deptList = deptService.list();

        return Result.success(deptList);
    }

    /**
     * 删除部门
     * @return
     */
    @Log
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("根据id删除部门:{}",id);
        //调用service删除部门
        deptService.delete(id);
        return Result.success();
    }

    /**
     * 新增部门
     * @param dept
     * @return
     */
    @Log
    @PostMapping
    public Result insert( @RequestBody Dept dept){
        log.info("新增部门:{}",dept);
        //调用service新增部门
        deptService.add(dept);

        return Result.success();
    }

    /**
     * 根据id查询部门
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result query(@PathVariable Integer id){
        log.info("根据id查询部门:{}",id);
        //调用service查询部门
//        List<Dept> deptList = deptService.query(id);
        Dept dept = deptService.query(id);
        return Result.success(dept);
    }

    @Log
    @PutMapping
    public Result alter(@RequestBody Dept dept){
        log.info("修改部门信息:{}",dept);
        //调用service修改部门信息
        deptService.alter(dept);

        return Result.success();
    }
}
